package avicit.bdp.dgs.mdm.rest;

import avicit.bdp.dgs.mdm.dto.TopicDTO;
import avicit.bdp.dgs.mdm.service.TopicService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-12-04
 * @类说明：JobRest
 * @修改记录：
 * @注意事项：
 * @主要功能：主题Rest接口
 */
@RestController
@Api(tags = "mdm", description = "主题Rest接口")
@RequestMapping("/api/bdp/dgs/mdm/topic")
public class TopicRest {
    @Autowired
    private TopicService topicService;

    /**
     * 新增主题
     *
     * @param topicDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增主题")
    public ResponseMsg<String> addTopic(@RequestBody TopicDTO topicDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.addTopic(topicDTO));

        return responseMsg;
    }

    /**
     * 删除/批量删除主题，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除主题，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteTopic(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.deleteTopic(id));

        return responseMsg;
    }

    /**
     * 修改主题
     *
     * @param topicDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改主题")
    public ResponseMsg<Boolean> updateTopic(@RequestBody TopicDTO topicDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.updateTopic(topicDTO));

        return responseMsg;
    }

    /**
     * 查询主题详情
     *
     * @param id 主题ID
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查询主题详情")
    public ResponseMsg<TopicDTO> getTopicDetail(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<TopicDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.getTopicDetail(id));

        return responseMsg;
    }

    /**
     * 分页查询主题
     *
     * @param keyWords
     * @param code
     * @param status
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "分页查询主题")
    public ResponseMsg<QueryRespBean<TopicDTO>> queryTopic(
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "编码", name = "code") @RequestParam(required = false) String code,
            @ApiParam(value = "状态", name = "status") @RequestParam(required = false) Integer status,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<TopicDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.queryTopic(keyWords, code, status, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 查询主题树型结构
     * 注意：
     * 1、模型管理模块使用，仅可查询启用状态的主题 && 已绑定模型的主题列表
     * 2、数据维护模块使用，仅可查询启用状态的主题 && 已绑定模型的主题列表
     *
     * @param keyWords
     * @param code
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "查询主题树")
    public ResponseMsg<List<TopicDTO>> queryTopicTree(
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "编码", name = "code") @RequestParam(required = false) String code) {
        ResponseMsg<List<TopicDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.queryTopicTree(keyWords, code));

        return responseMsg;
    }

    /**
     * 获取主题列表
     * 注意：
     * 1、数据维护模块使用，仅可查询启用状态的主题 && 已绑定模型的主题列表 && 模型表已发布的主题，否则未发布的模型会导致查询报错
     *
     * @param keyWords
     * @param code
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取主题列表")
    public ResponseMsg<List<TopicDTO>> getTopicList(
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "编码", name = "code") @RequestParam(required = false) String code) {
        ResponseMsg<List<TopicDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.getTopicList(keyWords, code));

        return responseMsg;
    }

    /**
     * 查询未使用的主题列表
     * 注意：新增模型时使用
     * 1、普通模型，仅能关联尚未被其他普通模型关联的主题，也即一个主题只能被一个普通模型关联，但可以关联多个其他模型
     * 2、仅可查询启用状态下的主题
     *
     * @param type     模型类型，0：普通模型
     * @param keyWords 主题名称
     * @param code     主题编码
     * @return
     */
    @GetMapping("/getUnusedTopic")
    @ApiOperation(value = "查询未使用的主题列表")
    public ResponseMsg<List<TopicDTO>> getUnusedTopicList(
            @ApiParam(value = "模型类型", name = "type") @RequestParam(defaultValue = "0", required = false) Integer type,
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "编码", name = "code") @RequestParam(required = false) String code) {
        ResponseMsg<List<TopicDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.getUnusedTopicList(type, keyWords, code));


        return responseMsg;
    }

    /**
     * 主题名称或编码是否重复
     *
     * @param id   主题ID，编辑时修改名称，需要传入topicId;新增场景不需要传入
     * @param name 主题名称
     * @param code 主题编码
     * @return
     */
    @GetMapping("/isSameName")
    @ApiOperation(value = "判读质量作业名称是否重复")
    public ResponseMsg<Boolean> isSameName(@ApiParam(value = "主题ID", name = "id") @RequestParam(required = false) String id,
                                           @ApiParam(value = "主题名称", name = "name") @RequestParam(required = false) String name,
                                           @ApiParam(value = "主题编码", name = "code") @RequestParam(required = false) String code) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.isSameName(id, name, code));

        return responseMsg;
    }

    /**
     * 启用/停用
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "启用/停用")
    @PostMapping("/updateStatus")
    public ResponseMsg<Boolean> updateStatus(@ApiParam(value = "主题ID", name = "id") @RequestParam(required = false) String id,
                                             @ApiParam(value = "状态", name = "status") @RequestParam(required = false) Integer status) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.topicService.updateStatus(id, status));

        return responseMsg;
    }
}
